<template>
  <div class="resignation-list-container">

    <div class="header">
      <h2 style="display: flex;"><router-link class="back-link" to="/main/renshi/renshizhidu/lizhisqd/lizhisqd">
          <i class="el-icon-arrow-left"></i>
          返回
        </router-link>离职申请表列表</h2>
      <div class="actions">

        <!-- <router-link to="/main/renshi/renshizhidu" class="btn-back">返回</router-link> -->
      </div>
    </div>

    <div class="search-bar">
      <div class="search-item">
        <label>姓名：</label>
        <input v-model="searchParams.name" placeholder="请输入姓名" @keyup.enter="handleSearch" />
      </div>
      <div class="search-item">
        <label>部门：</label>
        <input v-model="searchParams.department" placeholder="请输入部门" @keyup.enter="handleSearch" />
      </div>
      <button class="btn-search" @click="handleSearch">搜索</button>
      <button class="btn-reset" @click="resetSearch">重置</button>
      <router-link to="/main/renshi/renshizhidu/lizhisqd" class="btn-add">新增</router-link>
    </div>

    <div class="table-container">
      <el-table v-loading="loading" :data="tableData" border style="width: 100%"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }">
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="department" label="部门"></el-table-column>
        <el-table-column prop="position" label="职务"></el-table-column>
        <el-table-column prop="employeeId" label="工号"></el-table-column>
        <el-table-column prop="joinDate" label="进厂日期"></el-table-column>
        <el-table-column prop="leaveDate" label="离厂日期"></el-table-column>
        <el-table-column prop="resignType" label="离职类型"></el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleView(scope.row)" icon="el-icon-view">查看</el-button>
            <el-button size="mini" type="warning" @click="handleEdit(scope.row)" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="searchParams.current" :page-sizes="[10, 20, 50, 100]" :page-size="searchParams.size"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 删除确认对话框 -->
    <el-dialog title="确认删除" :visible.sync="deleteDialogVisible" width="30%">
      <div class="delete-confirm">
        <i class="el-icon-warning"></i>
        <p>确定要删除该离职申请记录吗？此操作不可恢复！</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="confirmDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getResignationApplicationList,
  deleteResignationApplication
} from '@/api/resignationApplication';
import { Message, MessageBox } from 'element-ui';

export default {
  name: 'ResignationApplicationList',
  data() {
    return {
      loading: false,
      tableData: [],
      total: 0,
      searchParams: {
        current: 1,
        size: 10,
        name: '',
        department: ''
      },
      deleteDialogVisible: false,
      currentDeleteId: null
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 获取列表数据
    async fetchData() {
      this.loading = true;
      try {
        console.log('获取离职申请列表，参数:', this.searchParams);
        const response = await getResignationApplicationList(this.searchParams);
        console.log('获取列表响应:', response);

        if (response.data && response.data.code === 200) {
          const { records, total } = response.data.data;
          this.tableData = records || [];
          this.total = total || 0;
          console.log('列表数据:', this.tableData);
        } else {
          console.error('获取列表失败:', response.data);
          Message.error(response.data?.message || '获取列表失败');
        }
      } catch (error) {
        console.error('获取列表错误:', error);
        Message.error('获取列表失败: ' + (error.message || '未知错误'));
      } finally {
        this.loading = false;
      }
    },

    // 搜索
    handleSearch() {
      this.searchParams.current = 1;
      this.fetchData();
    },

    // 重置搜索
    resetSearch() {
      this.searchParams = {
        current: 1,
        size: 10,
        name: '',
        department: ''
      };
      this.fetchData();
    },

    // 查看详情
    handleView(row) {
      this.$router.push({
        path: '/main/renshi/renshizhidu/lizhisqd/lizhisqd',
        query: { id: row.id, type: 'view' }
      });
    },

    // 编辑
    handleEdit(row) {
      this.$router.push({
        path: '/main/renshi/renshizhidu/lizhisqd/lizhisqd',
        query: { id: row.id, type: 'edit' }
      });
    },

    // 删除
    handleDelete(row) {
      this.currentDeleteId = row.id;
      this.deleteDialogVisible = true;
    },

    // 确认删除
    async confirmDelete() {
      if (!this.currentDeleteId) {
        Message.error('删除ID不能为空');
        return;
      }

      try {
        console.log('删除离职申请，ID:', this.currentDeleteId);
        const response = await deleteResignationApplication(this.currentDeleteId);
        console.log('删除响应:', response);

        if (response.data && response.data.code === 200) {
          Message.success('删除成功');
          this.fetchData();
        } else {
          console.error('删除失败:', response.data);
          Message.error(response.data?.message || '删除失败');
        }
      } catch (error) {
        console.error('删除错误:', error);
        Message.error('删除失败: ' + (error.message || '未知错误'));
      } finally {
        this.deleteDialogVisible = false;
        this.currentDeleteId = null;
      }
    },

    // 分页大小变化
    handleSizeChange(size) {
      this.searchParams.size = size;
      this.fetchData();
    },

    // 页码变化
    handleCurrentChange(current) {
      this.searchParams.current = current;
      this.fetchData();
    }
  }
};
</script>

<style scoped>
.resignation-list-container {
  padding: 20px;
  width: 1200px;
  margin: 0 auto;
  height: calc(100vh - 40px);
}

.back-link {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #409EFF;
  font-size: 14px;
  margin-right: 20px;
  transition: all 0.3s;
}

.back-link i {
  margin-right: 5px;
}

.back-link :hover {
  opacity: 0.8;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.actions {
  display: flex;
  gap: 10px;
}



.search-bar {
  display: flex;
  margin-bottom: 20px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 15px;
}

.search-item {
  display: flex;
  align-items: center;
}

.search-item label {
  margin-right: 5px;
  white-space: nowrap;
}

.search-item input {
  padding: 0px 8px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  width: 150px;
}

.btn-search,
.btn-reset,
.btn-add {
  display: inline-block;
  padding: 8px 15px;
  border-radius: 4px;
  text-decoration: none;
  font-size: 1em;
  border: 0px;
  font-family: '宋体', 'simsun';
}

.btn-search {
  background-color: #409eff;
  color: white;
}

.btn-reset {
  background-color: #909399;
  color: white;
}

.btn-add {
  background-color: #409eff;
  color: white;
}

/* ,.btn-back.btn-back {
  background-color: #909399;
  color: white;
} */
.table-container {
  margin-bottom: 20px;
}

.pagination {
  display: flex;
  justify-content: flex-end;
}

.delete-confirm {
  display: flex;
  align-items: center;
  gap: 10px;
}

.delete-confirm i {
  font-size: 24px;
  color: #e6a23c;
}
</style>